package com.ruoyi.points.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.points.domain.SysUserProfile;
import com.ruoyi.points.service.ISysUserProfileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分总数Controller
 * 
 * @author hh
 * @date 2025-08-18
 */
@RestController
@RequestMapping("/points/profile")
public class SysUserProfileController extends BaseController
{
    @Autowired
    private ISysUserProfileService sysUserProfileService;

    /**
     * 查询积分总数列表
     */

    //

    @PreAuthorize("@ss.hasPermi('points:profile:list')")
    @GetMapping("/userWith")
    public TableDataInfo userWith(@RequestParam(required = false) String userName, 
                                 @RequestParam(required = false) Integer points){
        startPage();
        SysUserProfile sysUserProfile = new SysUserProfile();
        sysUserProfile.setUserName(userName);
        sysUserProfile.setPoints(points);
        
        List<SysUserProfile> list = sysUserProfileService.selectSysUserProfileListWithUser(sysUserProfile);
        return getDataTable(list);
    }



    @PreAuthorize("@ss.hasPermi('points:profile:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserProfile sysUserProfile)
    {
        startPage();
        List<SysUserProfile> list = sysUserProfileService.selectSysUserProfileList(sysUserProfile);
        return getDataTable(list);
    }

    /**
     * 导出积分总数列表
     */
    @PreAuthorize("@ss.hasPermi('points:profile:export')")
    @Log(title = "积分总数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserProfile sysUserProfile)
    {
        List<SysUserProfile> list = sysUserProfileService.selectSysUserProfileList(sysUserProfile);
        ExcelUtil<SysUserProfile> util = new ExcelUtil<SysUserProfile>(SysUserProfile.class);
        util.exportExcel(response, list, "积分总数数据");
    }

    /**
     * 获取积分总数详细信息
     */
    @PreAuthorize("@ss.hasPermi('points:profile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysUserProfileService.selectSysUserProfileById(id));
    }

    /**
     * 新增积分总数
     */
    @PreAuthorize("@ss.hasPermi('points:profile:add')")
    @Log(title = "积分总数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserProfile sysUserProfile)
    {
        return toAjax(sysUserProfileService.insertSysUserProfile(sysUserProfile));
    }

    /**
     * 修改积分总数
     */
    @PreAuthorize("@ss.hasPermi('points:profile:edit')")
    @Log(title = "积分总数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserProfile sysUserProfile)
    {
        return toAjax(sysUserProfileService.updateSysUserProfile(sysUserProfile));
    }

    /**
     * 删除积分总数
     */
    @PreAuthorize("@ss.hasPermi('points:profile:remove')")
    @Log(title = "积分总数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysUserProfileService.deleteSysUserProfileByIds(ids));
    }

//修改会员类型
@PreAuthorize("@ss.hasPermi('points:profile:edit')")
    @PutMapping("/updateMemberType")
    public AjaxResult updateMemberType(@RequestBody SysUserProfile sysUserProfile)
    {
        // 检查用户是否只能修改自己的会员类型
        // 如果需要这个限制，可以取消注释下面的代码
        // if (!SecurityUtils.isCurrentUser(sysUserProfile.getUserId())) {
        //     throw new CustomException("只能修改自己的会员类型");
        // }
        return toAjax(sysUserProfileService.updateMemberTypeByUserId(sysUserProfile.getUserId(), sysUserProfile.getMemberType(), sysUserProfile.getMemberExpire()));
    }

    /**
     * 根据用户id查询用户档案
     */

    @GetMapping("/selectSysUserProfileByUserId")
    public AjaxResult selectSysUserProfileByUserId(@RequestParam Long userId)
    {
        return success(sysUserProfileService.selectSysUserProfileByUserId(userId));
    }
}
